@echo off

setlocal

rem *** Make sure the script is executing from the same directory as the MSIs ***
dir *.msi 1>nul 2>&1
if %errorlevel% neq 0 echo Fatal Error: You can only execute this script from within the same directory as the unzipped files && goto done

rem *** Get the script name ***
for /f "delims=" %%a in ('echo %0') do set scriptname=%%~na

rem *** Backup the main log file if there is one ***
if exist %scriptname%.log (erase %scriptname%.bak 2>nul && rename %scriptname%.log %scriptname%.bak)
erase *.log 2> nul

rem *** Get the current directory ***
set currentdir=%cd%

rem *** Make sure btstask.exe exists where we think it is ***
set btstask="C:\Program Files\Microsoft BizTalk Server 2006\BTSTask.exe"
if not exist %btstask% set btstask="C:\Program Files\Microsoft BizTalk Server 2004\BTSTask.exe"
if not exist %btstask% echo Fatal Error: "btstask.exe" could not be located. Please update this script with the correct path and rerun the script. && goto done

rem *** Make sure btscontrol.exe exists where we think it is ***
set btscontrol="C:\Program Files\Microsoft BizTalk Server 2006\BTSControl.exe"
if not exist %btscontrol% set btscontrol="C:\Program Files\Microsoft BizTalk Server 2004\BTSControl.exe"
if not exist %btscontrol% echo Fatal Error: "btscontrol.exe" could not be located. Please update this script with the correct path and rerun the script. && goto done

echo Partially stopping applications...
echo.
for /f "delims=" %%b in ('dir /b *.msi') do (echo Disabling Receive Locations for %%~nb && %btscontrol% PartialStopApp -ApplicationName:%%~nb 1>>"%currentdir%\%%~nb.log" 2>&1) || (type "%currentdir%\%%~nb.log">>"%currentdir%\%scriptname%.log" && goto failure)
echo ...Done partially stopping applications
echo.

echo Verifying that there are no suspended or running instances...
echo.
:loop
for /f "delims=" %%c in ('dir /b *.msi') do (echo Getting InstanceCounts for %%~nc && %btscontrol% GetInstanceCount -ApplicationName:%%~nc 1>>"%currentdir%\%%~nc.log" 2>&1) || (type "%currentdir%\%%~nc.log">>"%currentdir%\%scriptname%.log" && goto promptuserforaction)
echo ...Done checking for service instances
echo.

echo Completely stopping applications...
echo.
for /f "delims=" %%d in ('dir /b *.msi') do (echo Stopping Application %%~nd && %btscontrol% FullStopApp -ApplicationName:%%~nd 1>>"%currentdir%\%%~nd.log" 2>&1) || (type "%currentdir%\%%~nd.log">>"%currentdir%\%scriptname%.log" && goto failure)
echo ...Done completely stopping applications
echo.

echo Removing applications...
echo.
for /f "delims=" %%e in ('dir /b *.msi') do (echo Removing Application %%~ne && %btstask% RemoveApp -ApplicationName:%%~ne 1>>"%currentdir%\%%~ne.log" 2>&1) || (type "%currentdir%\%%~ne.log">>"%currentdir%\%scriptname%.log" && goto failure)
echo ...Done removing applications
echo.

echo Uninstalling applications...
echo.
for /f "delims=" %%f in ('dir /b *.msi') do (echo Uninstalling %%~nf && %btstask% UninstallApp -ApplicationName:%%~nf 1>>"%currentdir%\%%~nf.log" 2>&1) || (type "%currentdir%\%%~nf.log">>"%currentdir%\%scriptname%.log" && goto failure)
echo ...Done uninstalling BizTalk applications
echo.

echo Installing all the MSIs...
echo.
set msiexec="c:\windows\system32\msiexec.exe"
for /f "delims=" %%g in ('dir /b *.msi') do (echo Installing %%~ng && %msiexec% /qn /i %%g /log "%currentdir%\%%~ng.log" 1>>"%currentdir%\%scriptname%.log" 2>&1) || (type "%currentdir%\%%~ng.log">>"%currentdir%\%scriptname%.log" && goto failure)
echo ...Done installing MSIs
echo.

:success
echo.
echo Success!
echo The log can be found at "%cd%\%scriptname%.log".
goto done

:failure
echo.
echo Failure!
echo The log can be found at "%cd%\%scriptname%.log".
goto done

:promptuserforaction
echo.
echo There is at least one service instance left.
rem The CHOICE command is a built-in command on win 2003
rem but you must get it from the windows resource kit if you are running winxp
choice /c AR /m "[A]bort or [R]etry (continue waiting)?"
if errorlevel 2 goto loop
if errorlevel 1 (echo Aborting Deployment... && goto failure)

:done
endlocal
echo.
pause